dojo.provide("uws.Main");

//layout
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dojox.layout.ContentPane");
dojo.require("dijit.layout.TabContainer");

//application
dojo.require("dijit.Toolbar");
dojo.require("dijit.MenuBar");
dojo.require("dijit.PopupMenuBarItem");
dojo.require("dijit.Dialog");
dojo.require("dijit.form.Form");
dojo.require("dijit.form.Button");

var id_counter=0;
var username=null;

function hideLoader(){
	dojo.byId('loaderInner').innerHTML += "done.";
	setTimeout((function(){
		var loader = dojo.byId('loader');
		dojo.fadeOut({
			node: loader,
			duration: 1000,
			onEnd: function(){
				loader.style.display = "none";
			}
		}).play();
	}), 1000);
}

//id generator for HTML elements
function generateID(prefix){
	prefix = prefix || 'uws-gen';
	var id = prefix + (++id_counter);
	if(dojo.byId(id)){
		return generateId(id);
	}
	
	return id;
}

//send the upload form
function sendUploadFile(){
	dojo.require("dojo.io.iframe");
	
	dojo.addOnLoad(function(){
		dojo.io.iframe.send({
			form: "uploadForm",
			load: function (response, ioArgs) {
				if(response.search("PHP JSON decoding error")>=0){
					var tab = dijit.byId("uploadTab");
					if(tab) tab.set('content', response);
					else writeToStatusBar("The file you provided is does not contain a valid JSON string.");
				}else if(/\D/g.test(response)){
					var tab = dijit.byId("uploadTab");
					if(tab) tab.set('content', response);
					else writeToStatusBar("We experienced some server problem.");
				}else{
					openProject(parseInt(response));
				}
				
				return response;
			},
			error: function (response, ioArgs) {
				var tab = dijit.byId("uploadTab");
				if(tab) tab.set('content', response);
				else writeToStatusBar("We experienced some server problem.");
				return response;
			}
		});
	});
}

//trim functionality for trimming white space on the start and end of the string
function trim(str){
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');		//regex from http://blog.stevenlevithan.com/archives/faster-trim-javascript/comment-page-2
}

//for saving the altered custom attributes to the datastore
function rebuildCustomAttribute(item, customAttrName, newValue){
	var custom = "";
	var custom2 = dataStore.getValue(item,"custom").split("|");
	
	for(i=0;i<custom2.length; i++){
		if(trim(custom2[i]).length==0){
			continue;
		}
		var custom3 = custom2[i].split("#");
		
		if(custom3[0] == customAttrName){
			if(custom3[4] == "Boolean" && newValue==""){
				newValue="false";
			}
			custom += custom3[0] +'#'+ custom3[1] +'#'+ newValue +'#'+ custom3[3] +'#'+ custom3[4] +'#'+ custom3[5]+ '#'+ custom3[6] + "|"
		}
	}
	return custom;
}

//parse the custom attribute
function parseCustomAttribute(cAttribute){
	var customAttr = cAttribute.split("|");
	var retVal = [];
	
	for(var j=0; j< customAttr.length; j++){
		if(trim(customAttr[j]).length==0){
			continue;
		}
		
		var customItem = customAttr[j].split("#");
		
		retVal[customItem[0]] = customItem;
	}
	
	return retVal;
}

//check if the user wants to change his/her password
function changePassword(){
	if(dojo.byId("passwordCheckbox").checked){
		dijit.byId("passwordValidator").attr("disabled",false);
		dijit.byId("old").attr("disabled",false);
		dijit.byId("new").attr("disabled",false);
		dijit.byId("confirm").attr("disabled",false);
	}else{
		dijit.byId("passwordValidator").attr("disabled",true);
		dijit.byId("old").attr("disabled",true);
		dijit.byId("new").attr("disabled",true);
		dijit.byId("confirm").attr("disabled",true);
	}
}

function writeToStatusBar(text, duration){
	if(!duration){
		duration = 5000;
	}
	
	dojo.byId("statusBar").innerHTML=text;
	
	if(duration<=0){
		dojo.byId("statusBar").innerHTML=text;
	}else{
		setTimeout(function(){
			if(dojo.byId("statusBar").innerHTML==text){
				dojo.byId("statusBar").innerHTML="";
			}
		}, duration);
	}
}

function createPopUpDialog(title, content, style, buttons){
	var id = generateID("dialog");
	
	if(title==null || title==""){
		title="UML WebStudio";
	}
	
	if(content==null){
		content="";
	}
	
	if(style==null || style==""){
		style="width: 450px";
	}
	
	if(buttons==null){
		buttons = [];
		buttons[0]={label: "Ok", onclick: "this.destroy()"};
	}
	
	content+='<div class="center">';
	
	for(i=0;i<buttons.length;i++){
		buttons[i].onclick = buttons[i].onclick.replace("this.",'dijit.byId(\'' + id + '\').');
		
		content+='<button type="button" onclick="'+ buttons[i].onclick +'">'+ buttons[i].label + '</button>';
	}
	
	content+='</div>';
	
	var dialog = new dijit.Dialog({
		id:id,
		title: title,
		content: content,
		style: style
	});
	
	return id;
}

function offerToSave(){
	if(dataStore && dataStore.isDirty()){
		return confirm("The project is not yet saved. Continue to close the project?");
	}
	return true;
}


//submit values thru post method and open the results in a new window
function openWindowWithPost(url,name,keys,values){
	var newWindow = window.open(url, name); 
	if (!newWindow) return false;
	
	var html = "";
	html += "<html><head></head><body><form id='formid' method='post' action='" + url + "'>";
	
	if (keys && values && (keys.length == values.length)){
		for (var i=0; i < keys.length; i++){
			html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";
		}
	}
	
	html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</script></body></html>";
	newWindow.document.write(html);
	return newWindow;	
}

